package kali.wpj.kali.scan;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.github.johnpersano.supertoasts.SuperToast;
import com.github.johnpersano.supertoasts.util.Style;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.marshalchen.common.commonUtils.urlUtils.HttpUtilsAsync;

import org.apache.http.Header;

import java.io.ByteArrayInputStream;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Date;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import kali.wpj.kali.MainActivity;
import kali.wpj.kali.R;
import kali.wpj.kali.common.CircleProgress;
import kali.wpj.kali.globe.ServiceInfo;
import kali.wpj.kali.util.DialogBuilderTools;
import kali.wpj.kali.util.IPTools;
import kali.wpj.kali.util.JoomalStringHandle;
import kali.wpj.kali.util.SDCardUtils;

public class VulnerabilityScanningActivity extends Activity {
    private String url = ServiceInfo.VSURL;
    @InjectView(R.id.vs_scan)
    TextView txt_result;
    @InjectView(R.id.btn_vsdown)
    Button btn_down;
    @InjectView(R.id.vs_ip)
    EditText et_ip;
    @InjectView(R.id.btn_vsStart)
    Button btn_start;
    private String name;
    private Dialog mDialog;
    private DialogBuilderTools dailog;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_vulnerability_scanning);
        ButterKnife.inject(this);

    }

    @OnClick(R.id.btn_vsdown)
    public void downFile() {
        String sb = txt_result.getText().toString();
        byte bb[] = sb.getBytes();
        ByteArrayInputStream in = new ByteArrayInputStream(bb);
        SDCardUtils.writeToSDFromInput(SDCardUtils.getSDCardPath() + "/TimeFSafe", (new SimpleDateFormat("yyyyMMddHHmmss")).format(new Date()) + ".txt", in);
        SuperToast.create(VulnerabilityScanningActivity.this, getString(R.string.joomalsuccess), SuperToast.Duration.LONG,
                Style.getStyle(Style.BLUE, SuperToast.Animations.FLYIN)).show();


    }

    @OnClick(R.id.btn_vsStart)
    public void start() {
        String ip = et_ip.getText().toString().trim().replaceAll(" ", "");
        System.out.println("start----------------" + ip);
        if (IPTools.isIPAddress(ip) || IPTools.isWeb(ip)) {
            getResult(ip);
        } else {
            dailog = new DialogBuilderTools(VulnerabilityScanningActivity.this);
            dailog.dialogShow(getString(R.string.IPOrWebError));
            return;
        }
    }

    private void getResult(String targetIp) {
        HttpUtilsAsync.setTimeout(9999999);

        Log.i("----------", HttpUtilsAsync.getTimeout() + "");

        HttpUtilsAsync.get(url + targetIp + "/", new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int i, Header[] headers, byte[] bytes) {
                System.out.println(getString(R.string.ok) + bytes.toString());
                try {
                    name = ((new String(bytes, "UTF-8")));
                    name = JoomalStringHandle.StringDeal(name);
                    System.out.println("return values:" + name);
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
                txt_result.setText(name);
                btn_down.setVisibility(View.VISIBLE);
            }

            @Override
            public void onStart() {
            //    mProgressView.setVisibility(View.VISIBLE);
                showRoundProcessDialog(VulnerabilityScanningActivity.this, R.layout.loading_process_dialog_anim);

            }

            @Override
            public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {
                SuperToast.create(VulnerabilityScanningActivity.this, getString(R.string.faliCheck), SuperToast.Duration.LONG,
                        Style.getStyle(Style.BLUE, SuperToast.Animations.SCALE)).show();
            }

            @Override
            public void onFinish() {
                mDialog.dismiss();

            }

        });

    }

    public void showRoundProcessDialog(Context mContext, int layout) {
        mDialog = new AlertDialog.Builder(mContext).create();
        mDialog.setTitle(getString(R.string.search));
        mDialog.show();
        // 注意此处要放在show之后 否则会报异常
        mDialog.setContentView(layout);
    }

    @Override
    public void onBackPressed() {
        startActivity(new Intent(VulnerabilityScanningActivity.this, MainActivity.class));
        overridePendingTransition(R.anim.ac_transition_flip_horizontal_in, R.anim.ac_transition_flip_horizontal_out);
    }

}
